package com.hengxinguotong.zhihuichengjian.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.widget.view.StatusBarCompat;

/**
 * 忘记密码
 */
public class ForgetActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_password;
    private EditText et_phone;
    private EditText et_code;
    private EditText et_new_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        initView();

        //设置状态栏的颜色
        StatusBarCompat.compat(this, 0xff04b4ec);

    }

    private void initView() {
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        et_password = (EditText) findViewById(R.id.et_password);
        et_new_password = (EditText) findViewById(R.id.et_new_password);
        ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
        TextView tv_submit = (TextView) findViewById(R.id.tv_submit);
        TextView tv_get_code = (TextView) findViewById(R.id.tv_get_code);
        tv_submit.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        String phone = et_phone.getText().toString();
        String code = et_code.getText().toString();
        switch (view.getId()) {
            case R.id.tv_submit:
                String password = et_password.getText().toString();
                String new_password = et_new_password.getText().toString();
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code) || TextUtils.isEmpty(password) || TextUtils.isEmpty(new_password)) {
                    Toast.makeText(this, " 请填写完整 ", Toast.LENGTH_LONG).show();
                    return;
                }
                Boolean isRegisteruser = isRegisterNO(password);
                if (!isRegisteruser) {
                    Toast.makeText(this, " 密码格式不正确 ", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!password.equals(new_password)) {
                    Toast.makeText(this, " 两次输入的密码不同 ", Toast.LENGTH_LONG).show();
                    return;
                }


                break;
            case R.id.tv_get_code:
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(this, " 请输入手机号码 ", Toast.LENGTH_LONG).show();
                    return;
                }
                break;
            case R.id.iv_back:
                finish();
                break;
            default:
                break;


        }

    }

    private boolean isRegisterNO(String user) {

        String reg = "^[a-zA-Z0-9]{0,20}$";
        if (TextUtils.isEmpty(user)) return false;
        else return user.matches(reg);
    }
}
